/************************************************************************************
GamePad RT-Component
Copyright (c) 2010, Segway-Japan, Ltd.
All rights reserved.

Contact us if you use this software for sell.
If you use this software not for sell, you can use this software under BSD lisence.
See the files LICENSE.TXT and LICENSE-BSD.TXT for more details.                     
************************************************************************************/

#include <stdio.h>
#include "pad_win.h"


#pragma     comment(lib,"winmm.lib")

unsigned char axes = MAX_AXES;
unsigned char buttons = MAX_BUTTONS;


static bool usejoypad =false;//add
int *axis = NULL;
int *button = NULL;

int lx0 = 32767;
int ly0 = 32767;
int rx0 = 32767;
int ry0 = 32767;


#define NAME_LENGTH 512


UINT jid;
JOYINFOEX j_info;
JOYCAPS j_caps;

static unsigned int  button_id[] = { 
	PAD_SDL_JS_HAT_UP,
    PAD_SDL_JS_HAT_RIGHT,
    PAD_SDL_JS_HAT_DOWN,
    PAD_SDL_JS_HAT_LEFT,
	PAD_SDL_JS_HAT_L2,
	PAD_SDL_JS_HAT_R2,
	PAD_SDL_JS_HAT_L1,
	PAD_SDL_JS_HAT_R1,
	PAD_SDL_JS_HAT_START,
	PAD_SDL_JS_HAT_SELECT,
	PAD_SDL_JS_HAT_L3,
	PAD_SDL_JS_HAT_R3,
};



int pad_sdl_js_open(const char* device)
{
  MMRESULT res;

  if(strcmp(device, "/dev/input/js0")){
	  jid = JOYSTICKID1;
  }else if(strcmp(device, "/dev/input/js1")){
	  jid = JOYSTICKID2;
  }else{
	  printf("Invalid device : %s\n", device);
	  return -1;
  }

  joyGetDevCaps(JOYSTICKID1, &j_caps, sizeof(j_caps));

  j_info.dwSize = sizeof(JOYINFOEX);
  j_info.dwFlags = JOY_RETURNALL;

  res = joyGetPosEx(jid, &j_info);

  if(res == JOYERR_NOERROR ){
    printf("Found Joystick: %s\n", device);
	usejoypad=true;
  }else{
	printf("Fail to jotGetPos: %s \n", device);
	return -1;
  }

  button = (int *)malloc(sizeof(int) * buttons);
  axis = (int *)malloc(sizeof(int) * axes);

  return 0;
}

void pad_sdl_js_close()
{
  free(button);
  free(axis);
  usejoypad=false;
}

bool pad_sdl_js_isok()
{ 
  return( usejoypad); 
  //return true;
}

void pad_sdl_js_setok(bool b)
{
	usejoypad = b;
}

void pad_sdl_js_update()
{
	
  MMRESULT res;
  j_info.dwFlags = JOY_RETURNALL;
  res = joyGetPosEx(jid, &j_info);

  if(res == JOYERR_NOERROR ){
      for(int i=0; i< buttons; i++){
		  if( button_id[i] & j_info.dwButtons){
			button[i] = 1;
		  }else{
			button[i] = 0;
		  }
	  }
	  if(button[9] == 1){
	    lx0 = j_info.dwXpos;
	    ly0 = j_info.dwYpos;
	    rx0 = j_info.dwZpos;
	    ry0 = j_info.dwRpos;
	  }

	  axis[0] = j_info.dwXpos - lx0;
	  axis[1] = j_info.dwYpos - ly0;
	  axis[2] = j_info.dwZpos - rx0;
	  axis[3] = j_info.dwRpos - ry0;

  }else{
	  	printf("Fail to jotGetPosEx\n");
  }
  return;
}

int pad_sdl_js_axis(int n)
{

  if (axis) {
    return axis[n];
  }

  return 0;
}

float pad_sdl_js_axisf(int n)
{
  int j = pad_sdl_js_axis(n);

  return (float)((float)j / (float)PAD_SDL_JS_AXIS_MAX);
}

int pad_sdl_js_hat(int n)
{
  return 0;
}

int pad_sdl_js_button(int n)
{
  if (button && n < buttons) {
    return button[n];
  }
  return 0;
}

int pad_sdl_js_hat_push(int n)
{
  return 0;
}

int pad_sdl_js_button_push(int n)
{
  return 0;
}
